<?php

namespace App\Service\Index\v1_3_0;

use App\Helpers\AliyunUtil;
use App\Model\UserProfile;

/**
 * 首页弹窗
 */
class PopDetailService extends PopBaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $popType = $this->request->input('pop_type', '');
        //是否显示弹窗
        $showPop = !in_array($this->appId, explode(',', env('SHOW_POP_FORBID_APPID', '6')));
        //隐藏
        if (!$showPop) return [$popType => []];
        $ret = $this->packPopData($userId, $popType);
        check_dev_env() && log_user_result($userId, $ret);
        return $ret;
    }

    function packPopData($userId, $popType)
    {
        switch ($popType) {
            case 'new_call':
                $data = $this->packCommonData('新人通话券', '免费视频通话券', '立刻领取', ['minutes' => 1]);
                break;
            case 'call_video':
                $extra = ['host_list' => $this->getHostList()];
                $data = $this->packCommonData('她在等你视频哦～', '好无聊，哥哥跟我视频聊聊天吧～', '跟她视频聊聊', $extra);
                break;
            case 'new_recharge':
                return []; //$this->packRechargePop($userId);
                break;
            case 'old_recharge':
                return []; //$this->packCommonData('限时特惠', '', '开始翻牌');
                break;
            case 'host_invite':

                $rewardConfig = (config('act.invite.reward.recharge') + config('act.invite.act.0.reward_recharge')) * 100;

                $consumeConfig = config('act.invite.reward.consume') * 100;

                $extra = [
                    'recharge_text' => '可得'.$rewardConfig.'%充值返利',
                    'income_text' => '可得'.$consumeConfig.'%收益返利',
                    'recharge_title' => '好友充值',
                    'income_title' => '好友赚钱',
                    'recharge_percent' => $rewardConfig.'%',
                    'income_percent' => $consumeConfig.'%',
                ];
                $data = $this->packCommonData('邀请好友赚', '免费视频通话券', '立刻邀请赚现金', $extra);
                break;
            case 'check_msg':
                $data = $this->packCommonData('未读私信', '她给你发来了消息期待你的回复，别让她等太久哦～', '开始翻牌');
                break;
            case 'pub_moment':
                $data = $this->packCommonData('发布动态', '发布有趣的动态更能吸引ta的关注哦～', '去发布');
                break;
            case 'host_fix_profile1':
                $data = $this->packCommonData('完成真人认证，可获得官方认证标识', '需要上传真人头像哦～', '去真人认证');
                break;
            case 'host_fix_profile2':
                $data = $this->packCommonData('完成真人认证，可获得官方认证标识', '需要上传真人头像哦～', '去真人认证');
                break;
            case 'host_call_video':
                $data = $this->packCommonData('视频通话', '聊了那么久，不如视频跟他见见吧～', '马上邀请他视频通话～', ['tip' => '可使用1分钟免费通话券哦～']);
                break;

                //主播新手引导
            case 'host_new':
                $profile = UserProfile::getProfile($userId);
                $taskList = $this->getHostTaskList($userId, $profile);
		$data = [];
		//$data = $this->packCommonData('主播新手指南', '', '', ['task_list' => $taskList]);
                break;
                //男用户上传头像引导
            case 'user_avatar':
                $profile = UserProfile::getProfile($userId);
                if ($profile->avatar && strpos($profile->avatar, 'default') === false) $data = [];
                else {
                    $avatar = $profile->gender == 0 ? 'icons/female_sample.png' : 'icons/male_sample.png';
                    $data = $this->packCommonData('头像上传', '上传真人头像 +3金币，交友成功率提升300%', '编辑头像', ['avatar' => AliyunUtil::IMG_HOST_V2 . $avatar]);
                }
                break;
        }
        return [$popType => $data];
    }

    function getHostList()
    {
        $hostIds = UserProfile::getOnlineRecHosts(1);
        $hostIds = array_slice($hostIds, 0, 20);
        $profiles = UserProfile::getUsersProfile($hostIds);
        $list = [];
        foreach ($profiles as $profile) {
            $item = UserProfile::buildHostItem($profile);
            $item['is_real'] = $profile['avatar'] && $profile['avatar_vrfy'] ? 1 : 0;
            $list[] = $item;
        }
        return $list;
    }

    /**
     * 新手任务引导
     */
    function getHostTaskList($userId = 0, $profile = null)
    {
        if ((string)$profile->created_at < config('act.host_task_valid')) {
            log_user_result($profile->user_id, (string)$profile->created_at);
            return null;
        }
        $taskInfo = $this->checkHostTask($userId, $profile);
        //大礼包已经领取
        if ($taskInfo['big_gift'] == 2) return null;
        $hostTaskConf = config('act.host_task');
        $data = [];
        foreach ($hostTaskConf as $type => $item) {
            $rmb = $item['coin'] / 10;
            $data[] = [
                'title' => $item['title'],
                'reward' => "+{$rmb}元",
                'icon' => AliyunUtil::IMG_HOST_V2 . "icons/{$type}.png",
                'done' => $taskInfo[$type] ?? 2,
                'text' => "可领取红包{$item['coin']}金币（可提现）",
                'button_text' => $taskInfo[$type] == 1 ? '领取奖励' : '去完成',
                'jump_type' => $type
            ];
        }
        return $data;
    }
}
